package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import com.jiguang.shangcheng.dao.ShangChengDao;
import com.jiguang.shangcheng.pojo.Order;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/shang/shang_updateOrderState")
public class updateOrderState extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        //获取数据
        String id = req.getParameter("id");
        String state = req.getParameter("state");

        //通过订单ID修改订单状态
        ShangChengDao shangChengDao = new ShangChengDao();
        boolean result = shangChengDao.updateOrderState(id,state);
        if (result){
            //将商品下架，3支付完成
            if (state.equals("3")){
                //通过订单ID查询订单
                AdminDao adminDao = new AdminDao();
                Order order = adminDao.selectOrderByid(Integer.parseInt(id));
                //通过产品ID修改产品状态
                AdminDao.allowProduct(order.getProductId());
            }

            PrintWriter out = resp.getWriter();
            out.println("<script>alert('确认成功');location.href='/shang/shang_selectOrderInfo?id="+id+"';</script>");
            out.close();
        }else{
            PrintWriter out = resp.getWriter();
            out.println("<script>alert('确认失败');location.href='/shang/shang_selectOrder';</script>");
            out.close();
        }
    }
}
